package com.ruoyi.web.controller.system;


import java.util.Date;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BuildingInfo;
import com.ruoyi.system.service.IBuildingInfoService;

import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 楼宇信息Controller
 */
@RestController
@RequestMapping("/system/building")
public class BuildingInfoController extends BaseController {
    private String prefix = "system/building";

    @Autowired
    private IBuildingInfoService buildingInfoService;

    @GetMapping()
    public String building() {
        return prefix + "/building";
    }

    /**
     * 查询楼宇信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BuildingInfo buildingInfo) {
        startPage();
        List<BuildingInfo> list = buildingInfoService.selectBuildingInfoList(buildingInfo);
        return getDataTable(list);
    }

    /**
     * 新增楼宇信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存楼宇信息
     */
    @Log(title = "楼宇信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody BuildingInfo buildingInfo) {
        buildingInfo.setCreateTime(new Date());
        int a = buildingInfoService.insertBuildingInfo(buildingInfo);
        System.out.println(a);
        return AjaxResult.success(a);
    }

    /**
     * 修改楼宇信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        // 获取当前登录用户
        String username = SecurityUtils.getUsername();
        BuildingInfo buildingInfo = buildingInfoService.selectBuildingInfoById(id);
        buildingInfo.setUpdateBy(username);
        buildingInfo.setUpdateTime(new Date());
        mmap.put("buildingInfo", buildingInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存楼宇信息
     */
    @Log(title = "楼宇信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody BuildingInfo buildingInfo) {
        // 获取当前登录用户
        String username = SecurityUtils.getUsername();
        buildingInfo.setUpdateBy(username);
        buildingInfo.setUpdateTime(new Date());
        buildingInfo.setCreateTime(new Date());
        buildingInfo.setCreateBy(username);
        return toAjax(buildingInfoService.updateBuildingInfo(buildingInfo));
    }

    /**
     * 删除楼宇信息
     */
    @Log(title = "楼宇信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody String ids) {
        return toAjax(buildingInfoService.deleteBuildingInfoByIds(new Long[]{Long.parseLong(ids)}));
    }

    /**
     * 获取楼宇信息详细信息
     */
    @GetMapping("/detail/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(buildingInfoService.selectBuildingInfoById(id));
    }
} 